//
//  MoveCarInputViewController.m
//  carcareIOS
//
//  Created by 王然 on 16/9/13.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "MoveCarInputViewController.h"
#import "CarBrandView.h"
#import "CarCityNamesView.h"
#define TF_View_Height (50)

@interface MoveCarInputViewController ()

//车牌号签名的城市背景黑色view
@property (nonatomic , strong) UIView * cityNamesBackV;

@property (nonatomic , strong) CarCityNamesView * carCityV;

@property (nonatomic , strong) UIImageView * brandRightImageV;

@property (nonatomic , strong) BaseViewModel <MoveCarInputViewModelDelegate> * viewModel;

@property (nonatomic, strong) UITextField *telTextField;

@property (nonatomic , strong) CarBrandView * carBrandV;

@property (nonatomic , strong) UIView * headWhiteV;

@end

@implementation MoveCarInputViewController
- (instancetype)initWithViewModel:(BaseViewModel <MoveCarInputViewModelDelegate> *)viewModel
{
    self = [super initWithInputType:TYPE_INPUT_TABLEVIEW];
    if (self) {
        self.viewModel = viewModel;
        self.viewModel.viewController = self;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.headWhiteV];
    
    [self createUI];
    
    __weak __typeof(self) wself = self;
    [self navSetupBackItemWithIdentification:BACK_GRAY_ICON action:^{
        if(wself.willBackClick)wself.willBackClick(YES);
        [wself.navigationController popViewControllerAnimated:YES];
    }];
    
    
    [self.view addTapSingleAction:^(UITapGestureRecognizer *recognizer) {
        [wself clickOnBG];
    }];
    
    UIWindow * mainWindow = [UIApplication sharedApplication].delegate.window;
    
    [mainWindow addSubview:self.cityNamesBackV];
    
    self.cityNamesBackV.userInteractionEnabled = YES;
    self.cityNamesBackV.hidden = YES;
    
    [self.cityNamesBackV addTapSingleAction:^(UITapGestureRecognizer *recognizer) {
        [wself clickOnBG];
    }];
    
    self.carCityV = [[CarCityNamesView alloc] initWithFrame:CGRectMake(0, __MAIN_HEIGHT, __MAIN_WIDTH, 346)];
    self.carCityV.Choice = ^(NSString *name){
//        wself.canChangeVioCarModel.carinfoModel.carNoCityStr = name;
        if(wself.carCityV)wself.carBrandV.simpleCityL.text = name;
        if(wself.brandRightImageV)wself.brandRightImageV.transform = CGAffineTransformMakeRotation(0);
        [wself clickOnBG];
    };
    [self.cityNamesBackV addSubview:self.carCityV];
    self.carCityV.hidden = YES;
}
/**
 *  是否能右滑返回手势
 */
-(BOOL)viewControllerShouldGesturePopBack
{
    return NO;
}
-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDefault;
}
- (void)createUI
{
    CGFloat labelX = 15 * __MAIN_RATIO_375;
    UILabel * topTitleL = [[[UILabel labelWithFrame:CGRectMake(labelX, 36 * __MAIN_RATIO_375, __MAIN_WIDTH - labelX * 2, 23) font:FONT_DEFAULT_Bold(25 * __MAIN_RATIO_375) text:@"定制您专属的智能挪车卡" textColor:COLOR_HEXSTRING(@"#424242")] setupOnView:self.view] labelResetTextAlignment:NSTextAlignmentCenter];
    UILabel * topDetailL = [[[UILabel labelWithFrame:CGRectMake(topTitleL.left, topTitleL.bottom + labelX, topTitleL.width, topTitleL.height) font:FONT_DEFAULT_Light(13) text:@"他人可扫描挪车卡联系到您,信息绝对保密。" textColor:COLOR_HEXSTRING(@"#949494")] labelResetTextAlignment:NSTextAlignmentCenter] setupOnView:self.view];
    
    CGFloat textFieldX = 48.5 * __MAIN_RATIO_375;
    self.telTextField = [[self textFieldWithFrame:CGRectMake(textFieldX, topDetailL.bottom + 75 * __MAIN_RATIO_375, __MAIN_WIDTH - textFieldX * 2, TF_View_Height) type:TEXTFIELD_NORMAL] setupOnView:self.view];
    self.telTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.telTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.telTextField.placeholder = @"请输入您的手机号码";
    self.telTextField.font = FONT_DEFAULT_Light(15);
    self.telTextField.textColor = COLOR_HEXSTRING(@"#bdbdbd");
    
    self.carBrandV = [[[CarBrandView alloc]initWithFrame:CGRectMake(self.telTextField.left, self.telTextField.bottom, self.telTextField.width, self.telTextField.height)] setupOnView:self.view];
    [self registerTextField:self.carBrandV.brandTF];
    
    __weak __typeof(self) wself = self;
    self.carBrandV.cityVClick = ^()
    {//选择车牌所属城市
        [wself clickOnBG];
        [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            wself.cityNamesBackV.hidden = !wself.cityNamesBackV.hidden;
            
            wself.carCityV.hidden = !wself.carCityV.hidden;
            wself.carCityV.bottom = __MAIN_HEIGHT;
            wself.brandRightImageV.transform = CGAffineTransformMakeRotation(wself.carCityV.hidden ? 0 : M_PI);
            
        } completion:nil];
    };
    self.carCityV.hidden = YES;
    
    //生成挪车卡
    CGFloat okButtonHeight = 45;
    self.okButton = [ViewFactory buttonWithFrame:CGRectMake(self.telTextField.left, self.carBrandV.bottom + 41 * __MAIN_RATIO_H_667,self.telTextField.width, okButtonHeight)  title:@"生成挪车卡" normalColor:COLOR_HEXSTRING(@"#1F90E5") highlightColor:COLOR_HEXSTRING(@"#1F90E5") disabledColor:COLOR_HEXSTRING(@"#cdcdcd") inView:self.view action:^{
        [wself.viewModel clickOnCreateMoveCarCarTel:wself.telTextField.text brandCity:wself.carBrandV.simpleCityL.text brandNum:wself.carBrandV.brandTF.text];
    }];
    self.okButton.titleLabel.font = FONT_DEFAULT_Light(16);
    self.okButton.clipsToBounds = YES;
    self.okButton.layer.cornerRadius = 2;
    self.okButton.enabled = NO;
    
    //挪车卡使用说明
    CGFloat tipsBtnH = 30;
    UIButton * tipsBtn = [[UIButton buttonWithFrame:CGRectMake(self.telTextField.left, __MAIN_HEIGHT_64 - 20 * __MAIN_RATIO_375 - tipsBtnH, self.telTextField.width, tipsBtnH) title:@"" textColor:[UIColor clearColor] font:FONT_DEFAULT_Light(13) click:^{
        [wself.viewModel clickOnMoveCarInstructions];
        
    }] setupOnView:self.view];
    NSString * tipsStr = [NSString stringWithFormat:@"挪车使用说明"];
    NSMutableAttributedString * tipsAttributeStr = [[NSMutableAttributedString alloc]initWithString:tipsStr];
    NSRange tipsRange = NSMakeRange(0, [tipsStr length]);
    [tipsAttributeStr addAttribute:NSForegroundColorAttributeName value:COLOR_HEXSTRING(@"#1F90E5") range:tipsRange];
    [tipsAttributeStr addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:tipsRange];
    
    [tipsBtn setAttributedTitle:tipsAttributeStr forState:UIControlStateNormal];

    [self reloadSetData];
}
-(void)textFieldTextChanged:(UITextField *)textField
{
    self.okButton.enabled = (([self.telTextField.text isTel] && [self.carBrandV.brandTF.text isNotNil]) ? (self.carBrandV.brandTF.text.length == 6 ? ([Check isNumOrBigASCIILength:self.carBrandV.brandTF.text] ? YES : NO) : NO) : NO);
    
}
- (NSInteger)textFieldMaxNumberWithTextField:(UITextField *)textField
{
    if (textField == self.telTextField) {
        return 10;
    }
    else if (textField == self.carBrandV.brandTF)
    {
        return 5;
    }
    else{
        return [super textFieldMaxNumberWithTextField:textField];
    }
}
-(BOOL)textFieldCanAddString:(NSString *)string withTextField:(UITextField *)textField
{
    if (textField == self.carBrandV.brandTF) {
        if (![Check isNumOrBigASCII:string]) {
            return NO;
        }
    }
    return YES;
}
- (void)setMoveCarQRModel:(MoveCarQRModel *)moveCarQRModel
{
    _moveCarQRModel = moveCarQRModel;
    [self reloadSetData];
}
- (void)reloadSetData
{
    if (self.moveCarQRModel) {
        if([self.moveCarQRModel.phone isNotNil])self.telTextField.text = self.moveCarQRModel.phone;
        if ([self.moveCarQRModel.carno isNotNil] && self.moveCarQRModel.carno.length > 2)
        {
            self.carBrandV.simpleCityL.text = [self.moveCarQRModel.carno substringToIndex:1];
            self.carBrandV.brandTF.text = [self.moveCarQRModel.carno substringFromIndex:1];
        }
    }
}
#pragma mark 用于控制车牌号前面城市简称选择的控制
- (void)clickOnBG{
    [super clickOnBG];
    
    __weak __typeof(self) wself = self;
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        wself.carCityV.top = __MAIN_HEIGHT;
        wself.carCityV.hidden = YES;
        wself.cityNamesBackV.hidden = YES;
        if(wself.brandRightImageV)wself.brandRightImageV.transform = CGAffineTransformMakeRotation(0);
    } completion:nil];
}
- (void)keyBoardWillShow{
    __weak __typeof(self) wself = self;
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        wself.carCityV.top = __MAIN_HEIGHT;
        wself.carCityV.hidden = YES;
        wself.cityNamesBackV.hidden = YES;
        if(wself.brandRightImageV)wself.brandRightImageV.transform = CGAffineTransformMakeRotation(0);
    } completion:nil];
}
- (UIView *)cityNamesBackV
{
    if (!_cityNamesBackV) {
        _cityNamesBackV = [[UIView viewWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, __MAIN_HEIGHT)] resetBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.6]];
    }
    return _cityNamesBackV;
}
- (UIView *)headWhiteV
{
    if (!_headWhiteV) {
        _headWhiteV = [[UIView viewWithFrame:CGRectMake(0, -64, __MAIN_WIDTH, 70)] resetBackgroundColor:[UIColor whiteColor]];
    }
    return _headWhiteV;
}
#pragma mark - nav
-(UIColor *)navBackgroundColor{
    return [UIColor clearColor];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
